<template>
  <div>
    <el-row class="line-bottom" v-show="showSearch">
      <el-col class="form_col">
        <el-form :model="queryParams" ref="queryForm" key="meterHistoryQueryForm" :size="formItemSize"
                 v-show="showSearch"
                 :label-width="labelWidth">
          <div style="display: flex; justify-content: start; flex-wrap: wrap;">
            <el-form-item label="抄表时间" prop="freezeTimeRange">
              <el-date-picker :size="formItemSize"
                              v-model="freezeTimeRange"
                              type="daterange"
                              value-format="yyyy-MM-dd"
                              style="width: 220px;"
                              :clearable="false"
                              range-separator="至"
                              start-placeholder="开始日期"
                              end-placeholder="结束日期"
                              @change="handleQuery">
              </el-date-picker>
            </el-form-item>
          </div>
        </el-form>
      </el-col>

      <el-col class="form_query">
        <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
        <el-button @click="resetQuery" size="mini">重置</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <RightDropdown :dayColumns="historyColumns"
                     :showSearch.sync="showSearch" @queryTable="getList"
                     @export="handleExport"></RightDropdown>
    </el-row>

    <el-row>
      <el-table v-loading="loading" :data="historyList" border size="mini" :height="getAvailableHeight">
        <el-table-column label="#" type="index" align="center" width="40"/>
        <el-table-column label="用户名称" align="center" prop="userName" v-if="historyColumns[0].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="用户编号" align="center" prop="userId" v-if="historyColumns[1].visible"
                         :show-overflow-tooltip="true"/>
        <!--        <el-table-column label="门牌号" align="center" prop="roomName" v-if="historyColumns[2].visible"-->
        <!--                         :show-overflow-tooltip="true"/>-->
        <el-table-column label="设备编号" align="center" prop="terminalId" v-if="historyColumns[3].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="用户通讯地址" align="center" prop="userAddress" v-if="historyColumns[4].visible"
                         :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="抄表时间" align="center" prop="readDate" v-if="historyColumns[5].visible" width="180"
                         sortable="custom" :sort-orders="['descending', 'ascending']"
                         :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.readDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="水表读数(m³)" align="center" prop="totalFlow" v-if="historyColumns[6].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="终端电压(V)" align="center" prop="terminalVol" v-if="historyColumns[7].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="阀门状态" align="center" prop="valveState" v-if="historyColumns[8].visible"
                         :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.valveState === 1">开</span>
            <span v-if="scope.row.valveState === 0">关</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

  </div>
</template>

<script>
import moment from "moment/moment";
import {listData} from "@/api/dataquery/metered/meteredhistory.js";

export default {
  name: "History",
  data() {
    return {
      screenHeight: null,
      loading: false,
      // 显示搜索条件
      showSearch: true,
      collapseAble: false,
      formItemSize: 'mini',
      maxShow: 4,
      labelWidth: '80px',
      // 总条数
      total: 0,
      // 表格数据
      historyList: [],
      // 列信息
      historyColumns: [
        {key: 0, label: `用户名称`, visible: true},
        {key: 1, label: `用户编号`, visible: true},
        {key: 2, label: `门牌号`, visible: true},
        {key: 3, label: `设备编号`, visible: true},
        {key: 4, label: `通讯地址`, visible: true},
        {key: 5, label: `抄表时间`, visible: true},
        {key: 6, label: `水表读数`, visible: true},
        {key: 7, label: `终端电压`, visible: true},
        {key: 8, label: `阀门状态`, visible: true},
      ],
      freezeTimeRange: [
        new moment().subtract(1, 'day').format('YYYY-MM-DD'),
        new moment().subtract(1, 'day').format('YYYY-MM-DD')
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        // terminalId: null,
        locationNb: null,
        params: {
          beginReadDate: null,
          endReadDate: null
        },
      },
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? (this.fold ? 295 : 255) : 210)
    }
  },
  methods: {
    updateTerminalId(terminalId, locationNb) {
      // this.queryParams.terminalId = terminalId
      this.queryParams.locationNb = locationNb
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    // 重置表单
    resetQuery() {
      this.freezeTimeRange = [
        new moment().subtract(1, 'day').format('YYYY-MM-DD'),
        new moment().subtract(1, 'day').format('YYYY-MM-DD')
      ]
      // let terminalId = this.queryParams.terminalId
      let locationNb = this.queryParams.locationNb
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        command: null,
        sendTime: null,
        // terminalId: terminalId,
        locationNb: locationNb,
        params: {
          beginReadDate: this.freezeTimeRange[0],
          endReadDate: this.freezeTimeRange[0]
        },
      }
      this.handleQuery()
    },
    // 获取表格数据
    getList() {
      this.loading = true;
      this.queryParams.params.beginReadDate = this.freezeTimeRange[0];
      this.queryParams.params.endReadDate = this.freezeTimeRange[1];
      listData(this.queryParams).then(response => {
        if (response.code === 200) {
          this.historyList = response.rows
          this.loading = false;
          this.total = response.total
        } else {
          this.$modal.msgError("数据查看失败!")
          this.loading = false;
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datastatistics/meterhistory/export', {
        ...this.queryParams,
      }, `户表历史统计数据.xlsx`)
    }
  }

}
</script>

<style scoped lang="scss">
.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 20%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
